<?php
//
// Messages.class.php
//

// Direct access protection
if( !defined( 'LIRIOPE' )) die( 'Direct access is not allowed.' );

define( 'MESSAGE_DATE_FORMAT', "Y-m-d" );

class Messages extends obj {
  var $_;
  var $objMessages = array();
  var $params;
  var $errors;
  var $tracking='';

  public function __construct( $data, $params = array() )
  {
    // get the parameters
    $this->params = $params;
    // store the passed data
    $this->_ = $data;
  }
  
  public function setGATracking( $string=NULL ) {
    if( $string === NULL ) return false;
    $this->tracking = $string;
  }

  // store()
  // stores a whole array including recursion
  //
  public function store( $data ) {
    foreach( $data as $k => $v ) {
      if( is_array( $v )) { $this->store($v); }
      $this->set($k, $v);
    }
  }

  // set()
  // parameter storage
  // 
  public function set( $id=NULL, $value=NULL, $default=NULL ) {
    if( $id===NULL || $value===NULL ) return false;
    $this->params[$id] = empty( $value ) ? $default : $value;
    return $this;
  }

  // get()
  // parameter retrieval
  //
  public function get( $name = NULL, $default=FALSE ) {
    if ( $name===NULL ) return $this->params;
    return empty( $this->params[$name] ) ? $default : $this->params[$name];
  }

  public function hasTitle() { if($this->title) return TRUE; return FALSE; }
  public function getTitle() { return $this->title; }
  public function hasBumper() { if($this->video['bumper']) return TRUE; return FALSE; }
  public function getBumper() { return $this->video['bumper']; }
  public function hasCoverImage() { if($this->image['cover']) return TRUE; return FALSE; }
  public function getCoverImage() { return $this->image['cover']; }
  public function hasChipImage() { if($this->image['chip']) return TRUE; return FALSE; }
  public function getChipImage() { return $this->image['chip']; }
  public function hasMessages() { if($this->messages && count($this->messages) > 0) return TRUE; return FALSE; }
  public function getMessages() {
    // TODO: this is called from the view. I'd prefer to populate the message objects during construct
    $weeks = count($this->messages);
    foreach( $this->messages as $k=>$m ) {
      $week = $weeks - $k;
      $m = new Message( $m, $week );
      $this->objMessages[] = $m;
    }
    return $this->objMessages;
  }

  public function isCurrent() {
    // test the start and end days against a current timestamp
    $begins = strtotime( $this->begins );
    $ends = strtotime( $this->ends );
    $now = time();

    if( empty($begins) && empty($ends) ) return TRUE;

    if( empty($begins) && $now <= $ends ) return TRUE;
    if( empty($ends)   && $begins <= $now ) return TRUE;

    if( $begins <= $now && $now <= $ends ) return TRUE;

    return FALSE;
  }

}

class Message extends obj {
  public function __construct($data=NULL, $week=NULL) {
    if(empty($data)) return FALSE;
    $this->_ = $data;
    $this->week = $week;
  }

  public function hasTitle() { if( $this->title ) return TRUE; return FALSE; }
  public function getTitle() { return $this->title; }
  public function hasSpeaker() { if( $this->speaker ) return TRUE; return FALSE; }
  public function getSpeaker() { return $this->speaker; }
  public function hasDate() { if( $this->date ) return TRUE; return FALSE; }
  public function getDate() { return NRHCdate($this->date); }
  public function getShortDate() { return date( 'Y-m-d', strtotime($this->date)); }
  public function hasVideo() { if( $this->video ) return TRUE; return FALSE; }
  public function getVideo() { return "http://player.vimeo.com/video/".$this->video."?title=0&byline=0&portrait=0&color=8bc04e&autoplay=1&autostart=1"; }
  public function hasAudio() { if( $this->audio ) return TRUE; return FALSE; }
  public function getAudio() { return $this->audio; }
  public function getWeek() { return $this->week; }

  public function getVimeoData() {
    $vimeoAPIurl = 'http://vimeo.com/api/v2/video/%s.%s';
    $videoID = $this->video;
    $format = 'json';

    $url = sprintf($vimeoAPIurl, $videoID, $format);

    // get using CURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $output = json_decode(curl_exec($ch));
    curl_close($ch);

    return $output[0];
  }
}

?>
